package com.ruoyi.project.biz.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dyun.tool.mybatis.support.BaseEntityWrapper;
import com.ruoyi.project.biz.entity.BizUser;
import com.ruoyi.project.biz.entity.WxUser;
import com.ruoyi.project.biz.service.WxUserService;
import com.ruoyi.project.biz.vo.BizUserVO;
import net.dreamlu.mica.core.spring.SpringContextUtil;
import net.dreamlu.mica.core.utils.$;
import net.dreamlu.mica.core.utils.BeanUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户表(BizUser)表视图转换类
 *
 * @author dyun
 */
public class BizUserWrapper extends BaseEntityWrapper<BizUser, BizUserVO> {

    private static WxUserService wxUserService;

    static {
        wxUserService = SpringContextUtil.getBean(WxUserService.class);
    }

    public static BizUserWrapper build() {
        return new BizUserWrapper();
    }

    @Override
    public BizUserVO entityVO(BizUser bizUser) {
        BizUserVO bizUserVO = BeanUtil.copy(bizUser, BizUserVO.class);
        if ($.isNotNull(bizUserVO)) {
            List<WxUser> list = wxUserService.list(new LambdaQueryWrapper<WxUser>()
                    .eq(WxUser::getBizUserId, bizUser.getId()));
            if (list != null) {
                bizUserVO.setWxUser(list);
            }
        }
        return bizUserVO;
    }

    /**
     * 实体类集合包装
     *
     * @param list
     * @return
     */
    @Override
    public List<BizUserVO> listVO(List<BizUser> list) {
        return list.stream().map(this::entityVO).collect(Collectors.toList());
    }

    /**
     * 分页实体类集合包装
     *
     * @param pages
     * @return
     */
    @Override
    public IPage<BizUserVO> pageVO(IPage<BizUser> pages) {
        List<BizUserVO> records = listVO(pages.getRecords());
        IPage<BizUserVO> pageVo = new Page<>(pages.getCurrent(), pages.getSize(), pages.getTotal());
        pageVo.setRecords(records);
        return pageVo;
    }
}